package cn.xinfei.xdecision.common.service.analyse.job;

import cn.xinfei.xdecision.common.model.analyse.AnalyseRequestParam;
import cn.xinfei.xdecision.common.model.analyse.model.AnalyseInfoModel;
import cn.xinfei.xdecision.common.model.analyse.model.OperatorEnum;
import cn.xinfei.xdecision.common.model.analyse.vo.AnalyseDataDetail;
import cn.xinfei.xdecision.common.service.analyse.AnalyseInfoService;
import com.alibaba.nacos.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public abstract class AbstractOperator implements IOperator {

    @Autowired
    private AnalyseInfoService analyseInfoService;

    private OperatorEnum operator;

    public AbstractOperator(OperatorEnum operator) {
        this.operator = operator;
    }

    @Override
    public void start() {
        //start consumer
        AnalyseRequestParam requestParam = new AnalyseRequestParam();
        requestParam.setOperator(OperatorEnum.SUM.name());
        List<AnalyseInfoModel> analyseData = analyseInfoService.getAnalyseData(requestParam);
        if (CollectionUtils.isNotEmpty(analyseData)) {
            analyseData.forEach(info -> {
                //start consumer
            });
        }
    }

    @Override
    public void stop() {

    }

    public abstract void calculate(AnalyseInfoModel info, AnalyseDataDetail data);

    /**
     * primary_secondary_scene_operator_windows
     *
     * @return
     */
    protected String buildUniqueKey(AnalyseInfoModel info, AnalyseDataDetail data) {
        StringBuffer sb = new StringBuffer(data.getPrimary())
                .append("-").append(data.getSecondary())
                .append("-").append(info.getScene())
                .append("-").append(info.getOperator())
                .append("-").append(info.getWindows());
        String uniqueKey = sb.toString();

        return uniqueKey;
    }

}
